/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.montoroTurismo.beans;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.montoroTurismo.dao.AccesoBD;

/**
 *
 * @author ajdiaz
 */
public class ChargeTableOffer extends OffersBean {

    private List<OffersBean> offer;
    private OffersBean selectedOffer;
    private final FacesContext faceContext;
    private final HttpServletRequest httpServletRequest;
    private ResultSet rs;
    private AccesoBD db;
    private String username;

    public ChargeTableOffer() {

        offer = new ArrayList<OffersBean>();
        faceContext = FacesContext.getCurrentInstance();
        httpServletRequest = (HttpServletRequest) faceContext.getExternalContext().getRequest();
        username = httpServletRequest.getSession().getAttribute("sessionUsuario").toString();
        chargeOffers(username);
        populateOffers();
    }

    private void populateOffers() {
        try {
             java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd/MM/yyyy");
           String fechaFin;
             while (rs.next()) {
                  fechaFin = sdf.format(rs.getDate("fechaFin"));
                offer.add(new OffersBean(rs.getInt(1), rs.getString(2), fechaFin, rs.getString(4)));
            }

            db.close();
        } catch (SQLException ex) {
            Logger.getLogger(ChargeTableOffer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public OffersBean getSelectedOffer() {
        return selectedOffer;
    }

    public void setSelectedOffer(OffersBean selectedOffer) {
        this.selectedOffer = selectedOffer;
    }

    public List<OffersBean> getOffer() {
        return offer;
    }

    private void chargeOffers(String username) {

        try {
            db = new AccesoBD();
            rs = db.selectOfferByUsername(username);

        } catch (Exception ex) {
            Logger.getLogger(ChargeTableOffer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public String modificar() {
        String succes = "succes";
        httpServletRequest.getSession().setAttribute("offerTableForm", selectedOffer);
        httpServletRequest.getSession().setAttribute("accion", "modificar");
        return succes;
    }

    public String insertar() {
        String succes = "succes";
        httpServletRequest.getSession().setAttribute("accion", "insertar");
        return succes;
    }

    public String borrar() {
        FacesMessage msg = null;
        try {
            db = new AccesoBD();
            db.deleteOffer(selectedOffer.getId());
            chargeOffers(username);
            offer.clear();
            populateOffers();
            db.close();
        } catch (Exception ex) {
            Logger.getLogger(ChargeTableOffer.class.getName()).log(Level.SEVERE, null, ex);
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Error!", "Ha habido un error");
        }

        return null;
    }
}
